package com.foxit.pdf2pdf.web.common;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MultiThreadDownLoad {
	
	private String filePath = null;
	private String destination = null;
	
	public MultiThreadDownLoad(String filePath,String destination){
		this.filePath = filePath;
		this.destination = destination;
	}
	
	public void startDownload() {
        int threadNum = 5;  
        download(this.filePath, this.destination, threadNum);
    }  
  
    /**  
     */  
    private void download(String filePath, String destination, int threadNum) {
        try {  
            URL url = new URL(filePath);  
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
            conn.setRequestMethod("GET");  
            conn.setReadTimeout(5000);  
            if (conn.getResponseCode() == 200) {  
                int fileSize = conn.getContentLength();  
                String fileName = getFileName(filePath);  
                File file = new File(destination + File.separator + fileName);  
                RandomAccessFile raf = new RandomAccessFile(file, "rw");  
                raf.setLength(fileSize);  
                raf.close();  
                int block = fileSize % threadNum == 0 ? fileSize / threadNum  
                        : fileSize / threadNum + 1;  
                for (int threadId = 0; threadId < threadNum; threadId++) {  
                    new DownloadThread(threadId,block, file, url).start();  
                }  
  
            }  
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    private String getFileName(String filePath) {  
        return filePath.substring(filePath.lastIndexOf('/') + 1);  
    }  
  
}  
  
class DownloadThread extends Thread {  
    int start, end,threadId;  
    File file = null;  
    URL url = null;  
  
    public DownloadThread(int threadId,int block, File file, URL url) {  
        this.threadId = threadId;  
        start = block * threadId;  
        end = block * (threadId + 1) - 1;  
        this.file = file;  
        this.url = url;  
    }  
  
    public void run() {  
        try {  
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
            conn.setRequestMethod("GET");  
            conn.setReadTimeout(5000);  
            conn.setRequestProperty("Range", "bytes=" + start + "-" + end);  
            if (conn.getResponseCode() == 206) {  
                RandomAccessFile raf = new RandomAccessFile(file, "rw");  
                raf.seek(start);  
                InputStream inStream = conn.getInputStream();  
                byte[] b = new byte[1024];  
                int len = 0;  
                while ((len = inStream.read(b)) != -1) {  
                    raf.write(b, 0, len);  
                }  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}  
